package com.cupk.inspection.controller;

import com.cupk.inspection.model.InspectionRecord;
import com.cupk.inspection.service.InspectionRecordService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

@RestController
@RequestMapping("/api/records")
public class InspectionRecordController {

    private final InspectionRecordService recordService;

    public InspectionRecordController(InspectionRecordService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    public ResponseEntity<InspectionRecord> createRecord(@RequestBody InspectionRecord record) {
        InspectionRecord newRecord = recordService.createRecord(record);
        return new ResponseEntity<>(newRecord, HttpStatus.CREATED);
    }

    @GetMapping
    public ResponseEntity<Page<InspectionRecord>> getRecords(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long deviceId,
            @RequestParam(required = false) Timestamp start_time,
            @RequestParam(required = false) Timestamp end_time
    ) {
        Page<InspectionRecord> records = recordService.getRecords(page, size, deviceId, start_time, end_time);
        return ResponseEntity.ok(records);
    }

    @GetMapping("/{id}")
    public ResponseEntity<InspectionRecord> getRecord(@PathVariable Long id) {
        return recordService.getRecordById(id)
                .map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRecord(@PathVariable Long id) {
        recordService.deleteRecord(id);
        return ResponseEntity.noContent().build();
    }
}
    